package com.syni.mdd.yhd.common.all.utils;

import com.syni.mdd.sjb.common.component.utils.common.utils.SpringUtils;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessType;
import com.syni.mdd.yhd.common.all.service.BmsBusinessTypeService;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @className VideoUtils
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/26 15:34
 * @VERSION 1.0
 **/
public class VideoUtils {

    private static Set<Integer> userRandom = new HashSet<>();

    private static Set<Integer> businessRandom = new HashSet<>();

    private static Map<String,BmsBusinessType> bmsBusinessTypeMap = new HashMap<>();

    private static int randomSeeds = 1000000;

    private static int randomCompletion = 6;

    public static String getVideoNo(Integer tipClient,String tagName){
        System.out.println("userRandom--test : " + userRandom.size());
        System.out.println("businessRandom--test : " + businessRandom.size());
        Integer random;
        String videoNoPre = "YH";
        String tagIds = getTagId(tagName);
        if(tipClient == Constants.BUSINESS_CLIENT){
            videoNoPre = "SJ";
        }
        while (true) {
            random = (int) (Math.random() * randomSeeds);
            if(tipClient == Constants.USER_CLIENT){
                if (!userRandom.contains(random)) {
                    userRandom.add(random);
                    break;
                }
            }else if(tipClient == Constants.BUSINESS_CLIENT){
                if (!businessRandom.contains(random)) {
                    businessRandom.add(random);
                    break;
                }
            }
        }
        String randomQes = OrderUtils.completionNo(random.toString(),randomCompletion);
        SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd");
        String dateNo = formatter.format(new Date());
        String videoNo = videoNoPre + tagIds + dateNo + randomQes;
        return videoNo;
    }

    public static String getTagId(String tagName){
        final int max_num = 2;
        if(bmsBusinessTypeMap.containsKey(tagName)){
            return OrderUtils.completionNo(bmsBusinessTypeMap.get(tagName).getId().toString(),max_num);
        }
        BmsBusinessTypeService bmsBusinessTypeService = (BmsBusinessTypeService) SpringUtils.getBean("bmsBusinessTypeService");
        BmsBusinessType bmsBusinessType =  bmsBusinessTypeService.getBaseDao().findByTypeNameAndIsDelete(tagName,Constants.COMMON_FLAG_FALSE_INT);
        if(bmsBusinessType != null){
            bmsBusinessTypeMap.put(tagName,bmsBusinessType);
            return OrderUtils.completionNo(bmsBusinessType.getId().toString(),max_num);
        }
        return "00";
    }

    public static Set<Integer> getUserRandom() {
        return userRandom;
    }

    public static void setUserRandom(Set<Integer> userRandom) {
        VideoUtils.userRandom = userRandom;
    }

    public static Set<Integer> getBusinessRandom() {
        return businessRandom;
    }

    public static void setBusinessRandom(Set<Integer> businessRandom) {
        VideoUtils.businessRandom = businessRandom;
    }
}
